/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**Kingipüüdja
 *
 * @author Sirli, Raido, Argo
 */

import java.util.*;
import javax.microedition.lcdui.*;


public class Intro extends Canvas implements CommandListener {
    Joonistus midlet;
    Timer t;
    Random rand = new Random();
    private Image lahvatus = null;
    private Image masin = null;
    private Image taust2 = null;
    private Image katki = null;
    private Command cmExit,cmStart;  // Exit midlet
    static Image taust = null;

    int koord_X = getWidth()/2;
    int koord_Y = getHeight()/2;
    int korgus = getHeight();
    int suund = 0, suund1 = 0, suund2 = 0;
    int lint = 0;
    int i;
    int kinkidearv_konstant = 5, kingivahe_konstant = 40, mitu = 5, kiirus = 8;
    int[][] kingiandmed = new int[5][3];
    int[] kink = {0, 0, 0, 0, 0};
    int kingivahe = 5;
    int elud = 5, punktid = 0, kiiruse_lisaja = 0, kingi_lisaja = 0, kinke_lisatud = 0;
    int ajakulg = 0, lahenemine_majale = 40, plahvatus = 100, tiiger_tuleb = 120;
    int tiiger_piilub_X = getWidth(), tiiger_piilub_Y = getHeight();

    public Intro(Joonistus midlet){
        this.midlet = midlet;
        cmExit = new Command("Välju", Command.EXIT, 1);
        cmStart = new Command("Alusta", Command.ITEM, 1);
        addCommand(cmExit);
        addCommand(cmStart);
        setCommandListener(this);

        try{
            // Create immutable image
            lahvatus = Image.createImage("/Lahvatus.png");
            masin = Image.createImage("/Masin.png");
            katki = Image.createImage("/Katki.png");
            taust = Image.createImage("/taust.png");
            taust = MyImg.scaleImage(taust, getHeight(), getHeight());
            taust2 = Image.createImage("/Taust2.png");
            taust2 = MyImg.scaleImage(taust2, getHeight(), getHeight());

        } catch (java.io.IOException e){
            System.err.println("Unable to locate or read .png file"+e);
        }


    }

    protected void paint(Graphics g){
        g.setColor(255, 250, 250);
        g.setStrokeStyle(Graphics.DOTTED);
        if(ajakulg <= lahenemine_majale){
            g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
            g.drawImage(taust2, koord_X, koord_Y, Graphics.HCENTER | Graphics.VCENTER );
            g.drawString("Metsade ja mägede taga,", 10, 10, Graphics.TOP | Graphics.LEFT);
            g.drawString("suurte lumehangede vahel,", 10, 30, Graphics.TOP | Graphics.LEFT);
            g.drawString("asub jõuluvana kingikoda.", 10, 50, Graphics.TOP | Graphics.LEFT);
        }
        else{
            g.drawImage(taust, koord_X, koord_Y, Graphics.HCENTER | Graphics.VCENTER );
            if(lint == 0){
                lint = 1;
                g.drawImage(Louend.l_v_1, koord_X-35, koord_Y-10, Graphics.RIGHT| Graphics.TOP );
                g.drawImage(Louend.l_p_1, koord_X+35, koord_Y-10, Graphics.LEFT| Graphics.TOP );
                if(ajakulg <= plahvatus+4){
                    g.drawImage(masin, koord_X, koord_Y+80, Graphics.HCENTER| Graphics.VCENTER );
                    //g.drawString("Jõuluvana kingikojas.", koord_X-100, koord_Y-140, Graphics.TOP | Graphics.LEFT);
                }
                else{
                    g.drawImage(katki, koord_X, koord_Y+80, Graphics.HCENTER| Graphics.VCENTER );
                }
            }

            else{
                g.drawImage(Louend.l_v_2, koord_X-35, koord_Y-10, Graphics.RIGHT| Graphics.TOP );
                g.drawImage(Louend.l_p_2, koord_X+35, koord_Y-10, Graphics.LEFT| Graphics.TOP );
                if(ajakulg <= plahvatus+4){
                    g.drawImage(masin, koord_X+2, koord_Y+78, Graphics.HCENTER| Graphics.VCENTER );
                }
                else{
                    g.drawImage(katki, koord_X+2, koord_Y+78, Graphics.HCENTER| Graphics.VCENTER );
                }
                lint = 0;
            }

            for(i=0; i<kinkidearv_konstant; i++){
                if(kink[i]==1){
                    g.drawImage(Louend.kingipakk, kingiandmed[i][1], kingiandmed[i][2], Graphics.HCENTER| Graphics.BOTTOM);
                }
                if(kink[i]==0 && kingiandmed[i][2] >= koord_Y+102){
                    g.drawImage(Louend.kink_katki, kingiandmed[i][1], kingiandmed[i][2], Graphics.HCENTER| Graphics.BOTTOM);
                }
            }

            if(ajakulg >= lahenemine_majale && ajakulg <= plahvatus - 10){
                g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
                g.drawString("Jõuluvana kingikojas ...", 10, 10, Graphics.TOP | Graphics.LEFT);
            }
            else if(ajakulg >= plahvatus - 10 && ajakulg <= tiiger_tuleb + 20){
                g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
                g.drawString("    Aga äkki ...", 10, 30, Graphics.TOP | Graphics.LEFT);
            }

            if(ajakulg >= plahvatus && ajakulg <= plahvatus+4){
                g.drawImage(lahvatus, koord_X, koord_Y+30, Graphics.HCENTER| Graphics.VCENTER);
            }
            if(ajakulg >= plahvatus+2 && ajakulg <= plahvatus+6){
                g.drawImage(lahvatus, koord_X-30, koord_Y+20, Graphics.HCENTER| Graphics.VCENTER);
            }
            if(ajakulg >= plahvatus+4 && ajakulg <= plahvatus+8){
                g.drawImage(lahvatus, koord_X+30, koord_Y+40, Graphics.HCENTER| Graphics.VCENTER);
            }
            if(ajakulg >= tiiger_tuleb && ajakulg <= tiiger_tuleb + 20){
                g.drawImage(Louend.pyydja_keskel, tiiger_piilub_X, tiiger_piilub_Y, Graphics.LEFT| Graphics.TOP);
            }
            else if(ajakulg >= tiiger_tuleb + 20){
                g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
                g.drawImage(Louend.pyydja_vasak, tiiger_piilub_X, tiiger_piilub_Y, Graphics.LEFT| Graphics.TOP);
                g.drawString("Juhtus õnnetus...", 10, 10, Graphics.TOP | Graphics.LEFT);
                g.drawString("Aita Tiigrit ja", 10, 30, Graphics.TOP | Graphics.LEFT);
                g.drawString("püüa pakid kingikotti!", 10, 50, Graphics.TOP | Graphics.LEFT);
            }
        }
    }

    protected void keyPressed(int kood){
        if(getGameAction(kood)==Canvas.FIRE){
            midlet.display.setCurrent(midlet.louend);
            ajakulg = 0;
            korgus = getHeight();
            //taust3 = MyImg.scaleImage(taust2, korgus, korgus);
            tiiger_piilub_X = getWidth();
            tiiger_piilub_Y = getHeight();
        }
    }

    protected void pointerPressed(int x, int y){
        if(x>0 || y>0){
            midlet.display.setCurrent(midlet.louend);
            ajakulg = 0;
            korgus = getHeight();
            tiiger_piilub_X = getWidth();
            tiiger_piilub_Y = getHeight();
        }
    }

    public void showNotify(){
        t=new Timer();
        TimerTask tt=new TimerTask(){
            public void run(){
                liigu();
            }
        };
        t.schedule(tt, 500, 100); //tegevus, viivitus, intervall
    }

    public void hideNotify(){
        t.cancel();
    }

    void liigu(){
        ajakulg++;
            if(mitu != 0){
                for(i=0; i<kinkidearv_konstant; i++){
                    kingivahe = kingivahe + kiirus;
                    if(kink[i] == 0){
                        if(rand.nextInt(2) == 1 && kingivahe >= kingivahe_konstant){
                            if(rand.nextInt(2) == 0){
                                kingiandmed[i][0] = 0;
                                kingiandmed[i][1] = -20;
                                kingiandmed[i][2] = koord_Y-5;
                            }
                            else{
                                kingiandmed[i][0] = 1;
                                kingiandmed[i][1] = getWidth()+20;
                                kingiandmed[i][2] = koord_Y-5;
                            }
                            kink[i] = 1;
                            mitu--;
                            kingivahe = 0;
                        }
                        break;
                    }
                }
            }
            for(i=0; i<kinkidearv_konstant; i++){
                if(kink[i] == 1){
                    if(kingiandmed[i][1] >= (getWidth()/2)-36 && kingiandmed[i][1] <= (getWidth()/2)+36){
                        kingiandmed[i][2] = kingiandmed[i][2] + 15;
                        if(kingiandmed[i][0] == 0){
                            kingiandmed[i][1] = kingiandmed[i][1] + 4;
                        }
                        else{
                            kingiandmed[i][1] = kingiandmed[i][1] - 4;
                        }
                        if(kingiandmed[i][2] >= koord_Y+30 && kingiandmed[i][2] <= koord_Y+40 && ajakulg <= 100 || kingiandmed[i][2] >= koord_Y+30 && kingiandmed[i][2] <= koord_Y+40 && ajakulg <= 100){
                            kink[i] = 0;
                            mitu ++;

                            if(kingi_lisaja == 10 && kinke_lisatud <= 4){
                                kingi_lisaja = 0;
                                kinke_lisatud ++;
                                mitu ++;
                            }
                        }
                        else if(kingiandmed[i][2] >= koord_Y+102){
                            kink[i] = 0;
                            mitu ++;
                        }
                    }
                    else{
                        if(kingiandmed[i][0] == 0){
                            kingiandmed[i][1] = kingiandmed[i][1] + kiirus;
                        }
                        else{
                            kingiandmed[i][1] = kingiandmed[i][1] - kiirus;
                        }
                    }
                }
            }
            if(ajakulg >= tiiger_tuleb && ajakulg <= tiiger_tuleb+18){
                tiiger_piilub_X = tiiger_piilub_X - 4;
                tiiger_piilub_Y = tiiger_piilub_Y - 4;
            }

        //}
        repaint();
     }

     public void commandAction(Command c, Displayable d){
        if (c == cmExit){
            midlet.exitMIDlet();
        }
        if (c == cmStart){
            midlet.display.setCurrent(midlet.louend);
            Louend.elud = 5;
            Louend.punktid = 0;

        }
    }

}